package com.myblog.config;


import com.myblog.constant.RedisHashKey;
import com.myblog.constant.RedisKey;
import com.myblog.mapper.LikeMapper;
import com.myblog.mapper.VisitorMapper;
import com.myblog.model.Visitor;
import com.myblog.redis.RedisService;
import com.myblog.service.impl.RedisServiceImpl;
import com.myblog.service.impl.redis.HashRedisServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class ScheduledTask {

    @Autowired
    private VisitorMapper visitorMapper;

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private HashRedisServiceImpl hashRedisService;

    @Scheduled(cron = "0 0 0 * * ?")
    public void statisticsVisitorNum(){
        //获取数据库中的总访客量（获取昨日访客总量）
        long oldTotalVisitorNum = visitorMapper.selectTotalVisitorNum();
        //从redis中获取新的总访客量
        long newTotalVisitorNum = Long.parseLong(redisService.getHash(RedisKey.VISITOR, RedisHashKey.TOTAL_VISITOR).toString());
        //获取昨日访客量
        long yesterdayTotalVisitorNum = newTotalVisitorNum - oldTotalVisitorNum;
        //将数据写入redis
        hashRedisService.put(RedisKey.VISITOR,RedisHashKey.YESTERDAY_TOTAL_VISITOR,yesterdayTotalVisitorNum);
        //将redis中的数据同步到数据库中
        LinkedHashMap map = (LinkedHashMap) hashRedisService.getAllFieldAndValue(RedisKey.VISITOR);
        //获取hashKey，数据格式：  pageName：visitorNum
        for (Object o:map.keySet()) {
            String pageName = String.valueOf(o);
            Visitor visitor = visitorMapper.selectVisitorNumByPageName(pageName);
            if (!pageName.equals(RedisHashKey.YESTERDAY_TOTAL_VISITOR)) {
                if (visitor == null) {
                    visitorMapper.insertVisitor(Long.parseLong(map.get(o).toString()), pageName);
                }
                visitorMapper.updateVisitorByPageName(Long.parseLong(map.get(o).toString()), pageName);
            }
            //清除掉redis中除totalVisitor和yesterdayVisitor以外的的数据
            if (!RedisHashKey.TOTAL_VISITOR.equals(pageName) && !RedisHashKey.YESTERDAY_TOTAL_VISITOR.equals(pageName)){
                hashRedisService.hashDelete(RedisKey.VISITOR,pageName);
            }
        }

    }

    /**
     * 定时刷新点赞管理redis中点赞的数量玉数据库中的数量同步
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void articleThumbsUpRefresh(){
        //从数据库中勘察还剩多少未读点赞信息
        Integer numOfUnReadLikes = likeMapper.selectNumOfUnReadLikes();
        //从redis中获取未读点赞信息
        Object o = redisService.getHash(RedisKey.STATISTICS, RedisHashKey.ARTICLE_LIKE_NUM);
        Integer RedisNumOfUnReadLikes = Integer.parseInt(o.toString());
        //如果不一样，把数据库中未读信息存入redis
        if(!numOfUnReadLikes.equals(RedisNumOfUnReadLikes)){
            hashRedisService.put(RedisKey.STATISTICS, RedisHashKey.ARTICLE_LIKE_NUM,numOfUnReadLikes);
        }
    }

}
